/**
 *
 * jcrt and its source-code is licensed under the LGPL.
 * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
 * When copying, adapting, or redistributing this document in keeping with the guidelines above,
 * you are required to provide proper attribution to obinary.
 * If you reproduce or distribute the document without making any substantive modifications to its content,
 * please use the following attribution line:
 *
 * Copyright 2007 Atjava.com Ltd. (http://www.atjava.com) All rights reserved.
 *
 */
package com.atjava.jcrweb.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.atjava.jcrt.utility.Page;
import com.atjava.jcrweb.Constants;
import com.atjava.jcrweb.JcrWebException;
import com.atjava.jcrweb.info.UserInfo;
import com.atjava.jcrweb.manager.UserManager;
import com.atjava.jcrweb.manager.PackingToFlexHelper;
/**
 * 
 * 
 * @author biggie
 * @version $Revision: 1.11 $ $Date: 2008/10/14 08:24:21 $
 */
public class UserFlexServlet extends FlexBaseServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String process(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata)
			throws IOException, ServletException , JcrWebException , Exception {

		String type= this.getData(inputdata, "type");
		if(type.equals("createuser")){
			return createuser(request,response,inputdata);
		}
		if(type.equals("updateuser")){
			return updateuser(request,response,inputdata);
		}
		if(type.equals("deluser")){
			return deluser(request,response,inputdata);
		}
		if(type.equals("getuser")){
			return getuser(request,response,inputdata);
		}
		if(type.equals("login")){
			return login(request,response,inputdata);
		}
		if(type.equals("isLogin")){
			return isLogin(request,response,inputdata);
		}
		if(type.equals("logout")){
			HttpSession session = request.getSession();
			session.invalidate();
			return null;
		}
		if(type.equals("userlist")){
			return userlist(request,response,inputdata);
		}
		return null;
	}
	private String login(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{

		UserManager manager = UserManager.getInstance();
		String usercode = getData(inputdata,"userCode");
		String password = getData(inputdata,"password");
		String language = getData(inputdata,"language");

		boolean isAdmin = false;
		PackingToFlexHelper helper = new PackingToFlexHelper();
		HttpSession session = request.getSession();
		session.setAttribute(Constants.LANGUAGE_TYPE, language);
		if(Constants.ADMIN_USERNAME.equals(usercode) && Constants.ADMIN_PASSWORD.equals(password)){
			isAdmin = true;
		}
		if(!isAdmin){
			UserInfo info = manager.getUser(getData(inputdata,"userCode"));
			if(info!=null&&info.getPassword().equals(getData(inputdata,"password"))){
					session.setAttribute(Constants.LOGIN_USER_SESSION_KEY, info.getUserCode());
					session.setAttribute(Constants.LOGIN_USER_SESSION_INFO, info);
				helper.setResultSize("1");
				helper.setResultData("<isLogin>true</isLogin>");
			}else{
				helper.setResultSize("1");
				helper.setResultData("<isLogin>false</isLogin>");
			}
		}else{
			session.setAttribute(Constants.LOGIN_USER_SESSION_KEY, Constants.ADMIN_USERNAME);
			UserInfo info = new UserInfo();
			info.setUserCode(Constants.ADMIN_USERNAME);
			info.setUserName(Constants.ADMIN_USERNAME);
			info.setPassword(Constants.ADMIN_PASSWORD);
			session.setAttribute(Constants.LOGIN_USER_SESSION_INFO, info);
			helper.setResultSize("1");
			helper.setResultData("<isLogin>true</isLogin>");
		}
		return helper.toString();
	}
	private String isLogin(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		HttpSession session = request.getSession();
		PackingToFlexHelper helper = new PackingToFlexHelper();
		String language = (String)session.getAttribute(Constants.LANGUAGE_TYPE);
		
		if(session.getAttribute(Constants.LOGIN_USER_SESSION_KEY)!=null){
			String userCode = (String)session.getAttribute(Constants.LOGIN_USER_SESSION_KEY);
			if(Constants.ADMIN_USERNAME.equals(userCode)){
				helper.setResultSize("1");
				helper.setResultData("<isLogin>true</isLogin>" +
						"<language>"+language+"</language>" +
						"<userCode>"+Constants.ADMIN_USERNAME+"</userCode>" +
						"<userName>"+Constants.ADMIN_USERNAME+"</userName>");	
				
			}else{
				UserInfo info = (UserInfo)session.getAttribute(Constants.LOGIN_USER_SESSION_INFO);
				helper.setResultSize("1");
				helper.setResultData("<isLogin>true</isLogin><language>"+language+"</language>"+info.toXmlString());				
			}
		}else{
			helper.setResultSize("1");
			helper.setResultData("<isLogin>false</isLogin>");
		}
		return helper.toString();
	}
	
	private String getuser(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		UserManager manager = UserManager.getInstance();

		UserInfo info = manager.getUser(getData(inputdata,"userCode"));
		
		PackingToFlexHelper helper = new PackingToFlexHelper();
		StringBuffer sb = new StringBuffer();
		if(info!=null){
			helper.setResultSize("1");
			sb.append("<data>");
			sb.append(info.toXmlString());
			sb.append("<path>"+info.getPath()+"</path>");
			sb.append("</data>");
		}
		helper.setResultData(sb.toString());
		return helper.toString();
	}
	private String userlist(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		UserManager manager = UserManager.getInstance();

		List list = manager.getAllUsers();
		
		int page = 1;
		if(this.getData(inputdata, "page").length()>0){
			page = Integer.parseInt(this.getData(inputdata, "page"));
		}
		Page p = new Page(list,page,15);
		List resultList = p.getCurList();
		
		PackingToFlexHelper helper = new PackingToFlexHelper();
		StringBuffer sb = new StringBuffer();
		if(resultList!=null&&resultList.size()>0){
			helper.setResultSize(String.valueOf(resultList.size()));
			for(int i=0;i<resultList.size();i++){
				UserInfo info = (UserInfo)resultList.get(i);
				sb.append("<"+(i+1)+">");
				sb.append("<data>");
				sb.append(info.toXmlString());
				sb.append("<path>"+info.getPath()+"</path>");
				sb.append("<isfile>true</isfile>");
				sb.append("<picpath>images/file.gif</picpath>");
				sb.append("</data>");
				sb.append("</"+(i+1)+">");
			}
			sb.append("<curpage>"+page+"</curpage>");
			sb.append("<prepage>"+p.getPrePage()+"</prepage>");
			sb.append("<nextpage>"+p.getNextPage()+"</nextpage>");
			sb.append("<allpage>"+p.getPageNum()+"</allpage>");
			helper.setResultData(sb.toString());
		}
		return helper.toString();
	}
	
	private String createuser(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		UserManager manager = UserManager.getInstance();
		UserInfo info = new UserInfo();
		info.setUserName(getData(inputdata,"userName"));
		info.setUserCode(getData(inputdata,"userCode"));
		info.setPassword(getData(inputdata,"password"));
		info.setMail(getData(inputdata,"mail"));
		info.setNote(getData(inputdata,"note"));
		info.setPermissionDirPath(getData(inputdata,"perpath"));
		manager.createUser(info);
		return null;
	}
	
	private String updateuser(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception{
		UserManager manager = UserManager.getInstance();
		UserInfo info = new UserInfo();
		info.setUserName(getData(inputdata,"userName"));
		info.setUserCode(getData(inputdata,"userCode"));
		info.setPassword(getData(inputdata,"password"));
		info.setMail(getData(inputdata,"mail"));
		info.setNote(getData(inputdata,"note"));
		info.setPermissionDirPath(getData(inputdata,"perpath"));
		manager.updateUser(info);
		return null;
	}

	private String deluser(HttpServletRequest request,
			HttpServletResponse response, Map<String, String[]> inputdata) 
			throws IOException, ServletException , JcrWebException , Exception {
		UserManager manager = UserManager.getInstance();
		manager.deleteUser(getData(inputdata,"userCode"));
		return null;
	}
}
